package com.seedwill.common.dozer;

import org.dozer.ConfigurableCustomConverter;

/**
 * DOZER转换器
 * 
 * 日期转换
 * 
 * @author kdyi
 *
 */
public class StringAmousDateConverter extends ABaseConverterSupport implements
		ConfigurableCustomConverter {
	//
	private String fixedZZZ = "086";

	public String getFixedZZZ() {
		return fixedZZZ;
	}

	public void setFixedZZZ(String fixedZZZ) {
		this.fixedZZZ = fixedZZZ;
	}

	private String parameter;

	public String getParameter() {
		return parameter;
	}

	public void setParameter(String parameter) {
		this.parameter = parameter;
	}

	public Object convert(Object existingDestinationFieldValue,
			Object sourceFieldValue, Class<?> destinationClass,
			Class<?> sourceClass) {
		return convert(existingDestinationFieldValue, sourceFieldValue,
				destinationClass, sourceClass, parameter);
	}

	public Object convert(Object existingDestinationFieldValue,
			Object sourceFieldValue, Class destinationClass, Class sourceClass,
			String dateFormat) {
		Object result = null;
		if (sourceFieldValue != null) {
			try {
				if (org.apache.xmlbeans.XmlObject.class.isAssignableFrom(sourceClass)) {
					// 字符串转为日期
						org.apache.xmlbeans.XmlObject stObj = (org.apache.xmlbeans.XmlObject) sourceFieldValue;
						stObj.changeType(org.apache.xmlbeans.XmlString.type);
						String s = ((org.apache.xmlbeans.XmlString) sourceFieldValue)
								.getStringValue();
						if (s != null && s.trim().length() > 0) {
							if (fixedZZZ != null && s.endsWith(fixedZZZ)
									&& dateFormat.endsWith(fixedZZZ)) {
								s = s.substring(0, s.lastIndexOf(fixedZZZ));
								dateFormat = dateFormat.substring(0, dateFormat
										.lastIndexOf(fixedZZZ));
							}
							if (java.sql.Timestamp.class
									.isAssignableFrom(destinationClass)) {
								result = DateTimeConverterSupport.toTimestamp(
										dateFormat, s);
							} else if (java.sql.Date.class
									.isAssignableFrom(destinationClass)) {
								result = DateTimeConverterSupport.toSqlDate(
										dateFormat, s);
							} else if (java.sql.Time.class
									.isAssignableFrom(destinationClass)) {
								result = DateTimeConverterSupport.toTime(
										dateFormat, s);
							} else if (java.util.Calendar.class
									.isAssignableFrom(destinationClass)) {
								result = DateTimeConverterSupport.toCalendar(
										dateFormat, s);
							} else {
								result = DateTimeConverterSupport.toDate(
										dateFormat, s);
							}
						}
				} else {
					// 日期转为字符串
						String s = DateTimeConverterSupport.formatDate(dateFormat,
								(java.util.Date) sourceFieldValue);
						org.apache.xmlbeans.XmlString obj = org.apache.xmlbeans.XmlString.Factory
								.newInstance();
						obj.setStringValue(s);
						result = obj;
				}
			} catch (RuntimeException e) {
				log.warn(
						"Data convert exception[existingDestinationFieldValue:"
								+ existingDestinationFieldValue+(existingDestinationFieldValue !=null ? "("+existingDestinationFieldValue.getClass()+")" : "") + "]"
								+ "[sourceFieldValue:" + sourceFieldValue +(sourceFieldValue !=null ? "("+sourceFieldValue.getClass()+")" : "")+ "]"
								+ "[destinationClass:" + destinationClass + "]"
								+ "[sourceClass:" + sourceClass + "]", e);
				throw e;
			}
		}
		return result;
	}
}
